package cn.chengpengper.rpc.serialization.fst;

import cn.chengpengper.rpc.common.exception.SerializerException;
import cn.chengpengper.rpc.serialization.api.Serialization;
import cn.chengpengper.rpc.spi.annotation.SpiClass;
import lombok.extern.slf4j.Slf4j;
import org.nustaq.serialization.FSTConfiguration;

import java.util.Objects;

/**
 * @author chengpeng.hu
 */
@Slf4j
@SpiClass
public class FstSerialization implements Serialization {
    @Override
    public <T> byte[] serialize(T obj) {
        log.info("execute fst serialize");
        if (Objects.isNull(obj)) {
            throw new SerializerException("serialize object is null");
        }
        FSTConfiguration conf = FSTConfiguration.getDefaultConfiguration();
        return conf.asByteArray(obj);
    }

    @Override
    public <T> T deserialize(byte[] data, Class<T> clazz) {
        log.info("execute fst deserialize");
        if (Objects.isNull(data)) {
            throw new SerializerException("deserialize data is null");
        }
        FSTConfiguration conf = FSTConfiguration.getDefaultConfiguration();
        return (T) conf.asObject(data);
    }
}
